﻿using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using HtmlAgilityPack;
using System.Net;
using System.Web;
using System.Drawing;

namespace TULT
{
    public class LoLCounterApi
    {
        private readonly HtmlDocument _championPage = new HtmlDocument();
        private readonly HtmlDocument _generalChampionsPage = new HtmlDocument();
        private string _selectedChamp;

        /// <summary>
        /// This function loads the selected champions page, no get function cuz its converted into an URL
        /// </summary>
        public string SetChampion
        {
            get
            {
                return _selectedChamp;
            }
            set
            {
                using (WebClient client = new WebClient())
                {
                    // fix names before converting it into an URL
                    _selectedChamp = value;
                    _championPage.Load(new StringReader(client.DownloadString(@"http://www.lolcounter.com/champ/" + value.Replace("'", "").Replace(" ", "").Replace(".", "").ToLower())));
                }
            }
        }

        public string SubName
        {
            get { return _championPage.DocumentNode.SelectSingleNode("//*[@id='champion-name']/h4").InnerText.Trim(); }
        }

        /// <summary>
        /// Get the latest championlist from ChampionSelect.net
        /// </summary>
        public List<string> ChampionList
        {
            get
            {
                using (WebClient client = new WebClient())
                {
                    _generalChampionsPage.Load(new StringReader(client.DownloadString(@"http://www.lolcounter.com/champions")));
                    //*[@id="ahri"]/td[1]/span/a
                    //*[@id="akali"]/td[1]/span/a
                    //*[@id="alistar"]/td[1]/span/a

                    //*[@id="champions-grid"]/tbody/tr[2]/
                    //*[@id="champions-grid"]/tbody/tr[4]/

                    HtmlNodeCollection champList = _generalChampionsPage.DocumentNode.SelectNodes("//*[@id='counter-info-body']/table/tbody/tr/td/span/a");
                    List<string> stringList = new List<string>();
                    for (int i = 0; i < champList.Count; i++)
                    {
                        stringList.Add(HttpUtility.HtmlDecode(champList[i].InnerText.Trim()));
                    }
                    return stringList;
                }
            }
        }

        /// <summary>
        /// Get the champion icon of the selected champion
        /// </summary>
        public string ChampionImageInAscii
        {
            get
            {
                string url = @"http://www.solomid.net/guide/champ/" +
                             SetChampion.Replace("'", "").Replace(" ", "").Replace(".","").ToLower() + ".png";
                WebRequest client = WebRequest.Create(url);
                using (var response = client.GetResponse())
                using (var stream = response.GetResponseStream())
                {
                    if (stream != null)
                    {
                        var bitmap = Image.FromStream(stream) as Bitmap;
                        MemoryStream dataStream = new MemoryStream();
                        if (bitmap != null) bitmap.Save(dataStream, ImageFormat.Png);
                        byte[] b = dataStream.GetBuffer();
                        dataStream.Close();
                        return Convert.ToBase64String(b);
                    }
                    return "";
                }
            }
        }



        public List<string> CounterSummonerSpells
        {
            get
            {
                try
                {
                    HtmlNodeCollection counterNodes = _championPage.DocumentNode.SelectNodes("//*[@id='yw0_tab_1']/div[1]/div/a");
                    List<string> stringList = new List<string>();
                    for (int i = 0; i < counterNodes.Count; i++)
                    {
                        stringList.Add(HttpUtility.HtmlDecode(counterNodes[i].GetAttributeValue("data-title", "").Trim()));
                    }
                    return stringList;
                }
                catch (Exception)
                {
                    return new List<string>();
                }
            }
        }


        public List<string> CounterChampions
        {
            get
            {
                HtmlNodeCollection counterNodes = _generalChampionsPage.DocumentNode.SelectNodes(String.Format("//*[@id='{0}']/td[2]/ul/li", _selectedChamp.ToLower()));
                List<string> stringList = new List<string>();
                for (int i = 0; i < counterNodes.Count; i++)
                {
                    stringList.Add(HttpUtility.HtmlDecode(counterNodes[i].InnerText.Trim()));
                }
                return stringList;
            }
        }



        public List<string> CounterItems
        {
            get
            {
                try
                {
                    HtmlNodeCollection counterNodes =
                        _championPage.DocumentNode.SelectNodes("//*[@id='yw0_tab_1']/div[1]/a");
                    List<string> stringList = new List<string>();
                    for (int i = 0; i < counterNodes.Count; i++)
                    {
                        stringList.Add(HttpUtility.HtmlDecode(counterNodes[i].GetAttributeValue("data-title", "").Trim()));
                    }
                    return stringList;
                }
                catch (Exception)
                {
                    return new List<string>();
                }
            }
        }
        

        public List<string> CounterMechanics
        {
            get
            {
                try
                {
                    HtmlNodeCollection counterNodes =
                        _championPage.DocumentNode.SelectNodes("//*[@id='yw0_tab_1']/div[2]/p");
                    List<string> stringList = new List<string>();
                    for (int i = 0; i < counterNodes.Count; i++)
                    {
                        stringList.Add(HttpUtility.HtmlDecode(counterNodes[i].InnerText.Trim()));
                    }
                    return stringList;
                }
                catch (Exception)
                {
                    return new List<string>();
                }
            }
        }

        
        //*[@id="yw0_tab_1"]/div[2]/p[1]

        public List<string> BadAgainst
        {
            get
            {
                List<string> stringList = new List<string>();
                for (int i = 2; i < 6; i++)
                {
                    HtmlNode node = _championPage.DocumentNode.SelectSingleNode("//*[@id='counterpicks-list']/div[" + i + "]/a/h4");
                    stringList.Add(HttpUtility.HtmlDecode(node.InnerText.Trim()));
                }
                for (int i = 1; i < 7; i++)
                {
                    HtmlNode node = _championPage.DocumentNode.SelectSingleNode("//*[@id='showMoreCounters']/div[" + i + "]/a/h4");
                    stringList.Add(HttpUtility.HtmlDecode(node.InnerText.Trim()));
                }
                return stringList;
            }
        }


        public List<string> GoodAgainst
        {
            //*[@id="goodagainst-list"]/div[2]/a/h4
            //*[@id="goodagainst-list"]/div[5]/a/h4
            //*[@id="showMoreGoodAgainst"]/div[1]/a/h4
            //*[@id="showMoreGoodAgainst"]/div[6]/a/h4
            get
            {
                List<string> stringList = new List<string>();
                for (int i = 2; i < 6; i++)
                {
                    HtmlNode node = _championPage.DocumentNode.SelectSingleNode("//*[@id='goodagainst-list']/div[" + i + "]/a/h4");
                    stringList.Add(HttpUtility.HtmlDecode(node.InnerText.Trim()));
                }
                for (int i = 1; i < 7; i++)
                {
                    HtmlNode node = _championPage.DocumentNode.SelectSingleNode("//*[@id='showMoreGoodAgainst']/div[" + i + "]/a/h4");
                    stringList.Add(HttpUtility.HtmlDecode(node.InnerText.Trim()));
                }
                return stringList;
            }
        }

        public List<string> GoodWith
        {
            //*[@id="duopicks-list"]/div[2]/a/h4
            //*[@id="showMoreDuo"]/div[6]/a/h4
            get
            {
                List<string> stringList = new List<string>();
                for (int i = 2; i < 6; i++)
                {
                    HtmlNode node = _championPage.DocumentNode.SelectSingleNode("//*[@id='duopicks-list']/div[" + i + "]/a/h4");
                    stringList.Add(HttpUtility.HtmlDecode(node.InnerText.Trim()));
                }
                for (int i = 1; i < 7; i++)
                {
                    HtmlNode node = _championPage.DocumentNode.SelectSingleNode("//*[@id='showMoreDuo']/div[" + i + "]/a/h4");
                    stringList.Add(HttpUtility.HtmlDecode(node.InnerText.Trim()));
                }
                return stringList;
            }
        }

        public List<string> GetClass
        {
            //*[@id="champion-info-left"]/div[2]/span
            //*[@id="champion-info-left"]/div[2]
            //*[@id="champion-info-left"]/div[2]/span[1]
            get
            {
                try
                {
                    List<string> stringList = new List<string>();
                    HtmlNodeCollection classType =
                        _championPage.DocumentNode.SelectNodes("//*[@id='champion-info-left']/div[2]/span");
                    foreach (HtmlNode node in classType)
                    {
                        stringList.Add(HttpUtility.HtmlDecode(node.InnerText.Trim()));
                    }
                    return stringList;
                }
                catch (Exception)
                {
                    
                    return new List<string>();
                }
                
            }
        }


        public List<string> GetDamageType
        {
            //*[@id="champion-info-left"]/div[3]/span
            get
            {
                try
                {
                    List<string> stringList = new List<string>();
                    HtmlNodeCollection classType =
                        _championPage.DocumentNode.SelectNodes("//*[@id='champion-info-left']/div[3]/span");
                    foreach (HtmlNode node in classType)
                    {
                        stringList.Add(HttpUtility.HtmlDecode(node.InnerText.Trim()));
                    }
                    return stringList;
                }
                catch (Exception)
                {
                    
                    return new List<string>();
                }
                
            }
        }

        public List<string> GetRole
        {
            //*[@id="champion-info-left"]/div[3]/span
            get
            {
                try
                {
                    List<string> stringList = new List<string>();
                    HtmlNodeCollection classType =
                        _championPage.DocumentNode.SelectNodes("//*[@id='champion-info-left']/div[4]/span");
                    foreach (HtmlNode node in classType)
                    {
                        stringList.Add(HttpUtility.HtmlDecode(node.InnerText.Trim()));
                    }
                    return stringList;
                }
                catch (Exception)
                {
                    return new List<string>();
                }
                
            }
        }
    }
}